import { Middleware, Context, TabItem } from '../core'

/**
 *  渲染tabBar
 */
export default class TabBarParserMiddleware implements Middleware {
  async process(context: Context, next: () => Promise<void>): Promise<void> {
    try {
      const client = context.json.structData.clients.filter(c => c.id == context.json.clientId)[0]
      const platform = client.supportPlatforms.filter(s => s.platformName == 'Web浏览器')[0]
      const customize = platform.themes[0].content?.customize
      let subTitle = ''
      let tabbar: any[] = []
      Object.values(customize || []).forEach((c: any) => {
        if (typeof c == 'string') {
          subTitle = c
        } else {
          tabbar = c.list as any[]
        }
      })
      const tabs: TabItem[] = []
      tabbar.forEach((t: any) => {
        tabs.push({
          id: t.id,
          title: t.name,
          icon: t.icon,
          component: undefined,
          pageId: t.relPage.outerGraphicId
        })
      })
      context.tabInfo = tabs
      next()
    } catch (error) {
      throw new Error(`【渲染tabBar出错】- ${error}`)
    }
  }
}
